package top.cardone.context.util;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import org.springframework.core.annotation.AnnotationUtils;
import top.cardone.context.ApplicationContextHolder;
import top.cardone.context.annotation.Func;

import java.util.Map;

/**
 * Created by yao hai tao on 2015/8/24.
 */
public class FuncUtils {
    /**
     * 配置映射
     */
    private static Map<String, Map<String, Object>> configTable = Maps.newConcurrentMap();

    public static Map<String, Object> getMap(java.lang.reflect.Method method) {
        Func func = AnnotationUtils.findAnnotation(method, Func.class);

        return getMap(func);
    }

    public static Map<String, Object> getMap(Func func) {
        if (func == null) {
            return null;
        }

        if (org.apache.commons.lang3.StringUtils.isBlank(func.value())) {
            return null;
        }

        Map<String, Object> configMap = configTable.get(func.value());

        if (configMap == null) {
            configMap = ApplicationContextHolder.getBean(Gson.class).fromJson(func.value(), Map.class);

            configTable.put(func.value(), configMap);
        }

        return configMap;
    }
}
